﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows;

namespace Dynamic_UI_Framework
{
    [Serializable]
    public class UINode
    {
        public IVisual Target { get; set; }
        public string TypeName { get;set; }
        public string Name { get;set;  }
        public Dictionary<string, Object> Properties = new Dictionary<string, object>();
        public List<UINode> Children = new List<UINode>();
        public UINode Owner { get; set; }

        public virtual XElement WriteXml()
        {
            if (Target == null)
                return null;
            XElement ele = new XElement(this.TypeName);
            ele.SetAttributeValue("name", this.Name);

            foreach (var item in Properties)
            {
                if (item.Key.Equals("name"))
                    continue;

                ele.SetAttributeValue(item.Key, item.Value);
            }

            foreach (var item in Children)
            {
                ele.Add(item.WriteXml());
            }
            return ele;
        }
        public virtual void LoadXml(XElement ele)
        {
            this.TypeName = ele.Name.LocalName;
            this.Name = ele.Attribute("name").Value.ToString();

            foreach (var item in ele.Attributes())
            {
                if (item.Name.LocalName.Equals("name"))
                    continue;

                Properties.Add(item.Name.LocalName, item.Value);
            }

            foreach (var item in ele.Elements())
            {
                UINode child = new UINode();
                child.LoadXml(item);
                this.Children.Add(child);
            }
        }
    }
}
